// poj1062
// 题意：给定n(<=100)个商品，每个商品有一个费用以及等级，现在每个商品也
//       可以有k个优惠方案，就是如果购买了某个别的商品，购买该商品的价格
//       变为对应的优惠价格。
//       题目还有个条件，就是整个交易过程中，最高等级和最低等级不能超过m。
//       问最少花多少钱购买到1号商品。
//
// 题解：水题，随便搞搞。dijkstra。处理等级只需要枚举最高等级就行。
//   
// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

struct node { int to, cost; };

int const inf = 1 << 29;
int const maxn = 500;
int n, m;

typedef std::vector<std::vector<node> > graph_type;
typedef std::vector<node>::iterator edge_iter;

void add_edge(int x, int y, int z, graph_type & graph)
{
	graph[x].push_back({y, z});
}

int dijkstra(int source, int target, graph_type & graph)
{
	int dist[n + 4];
	std::fill(dist, dist + n + 4, inf);
	dist[source] = 0;
	typedef std::pair<int, int> cost_end;
	std::priority_queue<cost_end, std::vector<cost_end>, std::greater<cost_end> > pq;
	pq.push(std::make_pair(0, source));
	while (!pq.empty()) {
		cost_end tmp = pq.top(); pq.pop();
		int u = tmp.second;
		if (dist[u] < tmp.first) continue;

		for (edge_iter edge = graph[u].begin(); edge != graph[u].end(); ++edge) {
			int v = edge->to, c = edge->cost;
			if (dist[v] > dist[u] + c) {
				dist[v] = dist[u] + c;
				pq.push(std::make_pair(dist[v], v));
			}
		}
	}
	return dist[target];
}

struct data
{
	int cost, level, k;
	std::vector<std::pair<int, int> > discount; // index and cost
};

std::istream & operator>>(std::istream & is, data & a)
{
	is >> a.cost >> a.level >> a.k;
	a.discount.resize(a.k);
	for (int i = 0; i < a.k; i++)
		std::cin >> a.discount[i].first >> a.discount[i].second;
	return is;
}

std::vector<data> da;
bool tradable[maxn];

void build_graph(int x, graph_type & g)
{
	int level = da[x].level;
	for (int i = 1; i <= n; i++)
		tradable[i] = true;
	for (int i = 0; i < n; i++)
		if (da[i].level > level || da[i].level < level - m)
			tradable[i + 1] = false;

	for (int i = 0; i < n; i++) {
		if (!tradable[i + 1]) continue;
		add_edge(0, i + 1, da[i].cost, g);
		for (int j = 0; j < da[i].k; j++) {
			int v = da[i].discount[j].first, c = da[i].discount[j].second;
			if (!tradable[v]) continue;
			add_edge(v, i + 1, c, g);
		}
	}
}

int main()
{
	std::cin >> m >> n;
	da.resize(n);
	for (int i = 0; i < n; i++)
		std::cin >> da[i];

	int ans = inf;
	graph_type g(n + 1);
	for (int i = 0; i < n; i++) {
		for (int i = 0; i < n + 1; i++) g[i].clear();
		build_graph(i, g);
		ans = std::min(ans, dijkstra(0, 1, g));
	}
	std::cout << ans << '\n';
}

